/*
 * Getrap
 * Copyright 2008(C) Grzegorz 'milyges' Glinski
 *
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
*/
#include <fSettings.h>
#include <config.h>
#include <QtDebug>
#include <QFileDialog>
#include <QMessageBox>

FSettings::FSettings(QWidget * parent, Qt::WFlags f) : QDialog(parent, f)
{
 ui.setupUi(this);
 /* Laczymy sygnaly */
 connect(ui.cbUsePremium, SIGNAL(stateChanged(int)), this, SLOT(cbPremiumChanged(int)));
 connect(ui.bntBrowse, SIGNAL(clicked()), this, SLOT(selectDir()));
 /* Ladujemy konfiguracje */
 ui.textOutputDir->setText(Config::getValue("OutputDir"));
 ui.textLogin->setText(Config::getValue("PremiumLogin"));
 ui.textPassword->setText(Config::getValue("PremiumPassword"));
 ui.cbUsePremium->setCheckState((Config::getValue("UsePremium").toInt() != 0) ? Qt::Checked : Qt::Unchecked);
 ui.cbAutoStart->setCheckState((Config::getValue("Autostart").toInt() != 0) ? Qt::Checked : Qt::Unchecked);
 ui.sbThreads->setValue(Config::getValue("PremiumThreads").toInt());
 cbPremiumChanged(ui.cbUsePremium->checkState());
}

FSettings::~FSettings()
{
 /* Aktualizujemy konfiguracje */
 Config::setValue("OutputDir", ui.textOutputDir->text());
 Config::setValue("PremiumLogin", ui.textLogin->text());
 Config::setValue("PremiumPassword", ui.textPassword->text());
 Config::setValue("UsePremium", (ui.cbUsePremium->checkState() == Qt::Checked) ? QString("1") : QString("0"));
 Config::setValue("Autostart", (ui.cbAutoStart->checkState() == Qt::Checked) ? QString("1") : QString("0"));
 Config::setValue("PremiumThreads", QString("%1").arg(ui.sbThreads->value()));
 /* Zapisujemy ustawienia */
 Config::save();
}

void FSettings::cbPremiumChanged(int state)
{
 bool enabled = (state != Qt::Unchecked);

 if (enabled)
 {
  ui.cbUsePremium->setCheckState(Qt::Unchecked);
  QMessageBox::information(this, tr("Informacja"), tr("Obsługa kont premium nie została zaimplementowana"));
  return;
 }
 /* Aktywujemy / dezaktywujemy odpowiednie pola */
 ui.textLogin->setEnabled(enabled);
 ui.textPassword->setEnabled(enabled);
 ui.sbThreads->setEnabled(enabled);
 ui.label->setEnabled(enabled);
 ui.label_2->setEnabled(enabled);
 ui.label_4->setEnabled(enabled);
}

void FSettings::selectDir(void)
{
 QFileDialog * wnd = new QFileDialog(this);
 wnd->setAcceptMode(QFileDialog::AcceptOpen);
 wnd->setFileMode(QFileDialog::DirectoryOnly);
 wnd->exec();
 if (wnd->result() == QDialog::Accepted)
    ui.textOutputDir->setText(wnd->selectedFiles().at(0).toLocal8Bit().constData());
 delete wnd;
}
